package guinong.com.gnw.ui.widged;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import guinong.com.gnw.R;
import guinong.com.gnw.port.MainUsedInterface;


public class TitleBar extends LinearLayout implements OnClickListener {
    private boolean isShowSearchView = false;//是否显示搜索框
    private TextView mTitle = null;//标题
    //   private SearchView mSearchView = null;//搜索框
    private Button mLeftTextBtn = null;//左标题按钮
    //private Button mRightTextBtn = null;//右标题按钮
    private ImageButton mLeftImageBtn = null;//左图片按钮
    private TextView mRightImageBtn = null;//右图片按钮
    private MainUsedInterface.MainButtonClickListener mMainClickListener = null;//按键监听
    private String mLeftButtonName = "";//左按钮内容
    private String mRightButtonName = "";//右按钮内容
    RelativeLayout titilBackgroud;
    LinearLayout mRlLeft;

    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initUI(context);
    }

    public TitleBar(Context context) {
        super(context);
        initUI(context);
    }

    //点击监听，用类实现MainButtonClickListener接口作为监听者
    public void setMainClickListener(MainUsedInterface.MainButtonClickListener listener, String leftButtonName, String rightButtonName) {
        mMainClickListener = listener;
        mLeftButtonName = leftButtonName;
        mRightButtonName = rightButtonName;
    }

    //解析布局
    private void initUI(Context context) {
        LayoutInflater mInflater = LayoutInflater.from(context);
        View view = mInflater.inflate(R.layout.titlebar_view, null);
        this.addView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        mTitle = (TextView) view.findViewById(R.id.title);
        mLeftTextBtn = (Button) view.findViewById(R.id.left_text_btn);
       // mRightTextBtn = (Button) view.findViewById(R.id.right_text_btn);
        mLeftImageBtn = (ImageButton) view.findViewById(R.id.left_image_btn);
        mRightImageBtn = (TextView) view.findViewById(R.id.right_image_btn);
        titilBackgroud = (RelativeLayout) view.findViewById(R.id.rl_title);
        mRlLeft = (LinearLayout) view.findViewById(R.id.ll_left);
    }

    /**
     * 修改背景颜色
     *
     * @param color
     */
    public void setTitleBackGroud(int color) {
        titilBackgroud.setBackgroundResource(color);
    }

    /**
     * 修改字体颜色
     *
     * @param color
     */
    public void setRightTextColor(int color) {
       // mRightTextBtn.setTextColor(color);
    }

    //初始化控件，标题、左侧文字，右侧文字，搜索栏是否显示
    public void initTitleBarInfo(String title, String leftBtnText, String rightBtnText, boolean isShowSearch) {
        mTitle.setText(title);
        if (!leftBtnText.equals("")) {
            mLeftTextBtn.setText(leftBtnText);
            mLeftTextBtn.setVisibility(View.VISIBLE);
            if (mLeftTextBtn.getText().toString().length() > 2) {
                mLeftTextBtn.setPadding(0, 0, 16, 0);
            }
            mLeftTextBtn.setOnClickListener(this);
        } else {
            mLeftTextBtn.setText("");
            mLeftTextBtn.setVisibility(View.GONE);
        }

      /*  if (!rightBtnText.equals("")) {
            mRightTextBtn.setText(rightBtnText);
            mRightTextBtn.setVisibility(View.VISIBLE);
            mRightTextBtn.setOnClickListener(this);
        } else {
            mRightTextBtn.setText("");
            mRightTextBtn.setVisibility(View.GONE);
        }*/
    }

    /**
     * 设置右边的文字
     *
     * @param name
     */
    public void setRightBtnText(String name) {
        mRightImageBtn.setText(name);
    } /**
     * 设置右边的文字
     *
     * @param name
     */
    public void setTitle(String name) {
        mTitle.setText(name);
    }

    /**
     * 车库图标更改  地图模式和列表模式切换
     * 和监听的name
     *
     * @param src
     */
    public void setRightImage(int src) {
        mRightImageBtn.setBackgroundResource(src);
        mRightImageBtn.setOnClickListener(this);
    }

    //初始化标题
    public void initTitleBarInfo(String title, String leftBtnText, int leftImageId, String rightBtnText, int rightImageId, boolean isShowSearch) {
        mTitle.setText(title);
        mRlLeft.setOnClickListener(this);
        mRightImageBtn.setOnClickListener(this);
        if (!leftBtnText.equals("")) {
            mLeftTextBtn.setText(leftBtnText);
            mLeftTextBtn.setVisibility(View.VISIBLE);
            mLeftTextBtn.setOnClickListener(this);
            if (mLeftTextBtn.getText().toString().length() > 2) {
                mLeftTextBtn.setPadding(0, 0, 16, 0);//padding-bottom=16
            }
        } else if (leftImageId > 0 || leftImageId == 1) {
            mLeftTextBtn.setText("");
            mLeftTextBtn.setVisibility(View.GONE);
            mLeftImageBtn.setVisibility(View.VISIBLE);
            //  mLeftImageBtn.setBackgroundResource(leftImageId);
            mLeftImageBtn.setOnClickListener(this);
        } else {
            mLeftTextBtn.setText("");
            mLeftTextBtn.setVisibility(View.GONE);
            mLeftImageBtn.setVisibility(View.GONE);
        }


        if (!rightBtnText.equals("")) {
          /*  mRightTextBtn.setText(rightBtnText);
            mRightTextBtn.setVisibility(View.VISIBLE);
            mRightTextBtn.setOnClickListener(this);*/

        } else if (rightImageId > 0) {
           /* mRightTextBtn.setText("");
            mRightTextBtn.setVisibility(View.GONE);*/
            mRightImageBtn.setVisibility(View.VISIBLE);
            mRightImageBtn.setOnClickListener(this);
            mRlLeft.setOnClickListener(this);
        } else {
          /*  mRightTextBtn.setText("");
            mRightTextBtn.setVisibility(View.GONE);*/
            mRightImageBtn.setVisibility(View.GONE);
        }
    }

    /**
     * 设置控件显示和字体颜色
     *
     * @param isShow
     */

    public void setRightTextBtnShow(boolean isShow) {
        if (isShow) {
          //  mRightTextBtn.setText("");
        }
    }

    public void updateTitle(String newTitle) {
        mTitle.setText(newTitle);
    }

    public String getTitle() {
        return mTitle.getText().toString();
    }

    @Override
    public void onClick(View v) {
        if (mMainClickListener == null) {
            return;
        }
        if (v.getId() == R.id.left_image_btn || v.getId() == R.id.left_text_btn || v.getId() == R.id.ll_left) {
            // Left button clicked
            mMainClickListener.clickButtonListener(mLeftButtonName, v);
        } else {
            // Right button clicked
            mMainClickListener.clickButtonListener(mRightButtonName, v);
        }
    }
    /**
     * 执行构造函数后，执行setMainClickListener（listener,leftButtonName,rightButtonName）
     * 以及initTitleBarInfo(title,leftbtn/leftImg,rightbtn/rightImg,showSearchView)
     */
}
